/*
 * List Reports Module Page JS
 * This creates the admin GUI List Reports Page for the Reports Module
 *
 * JSLint.com Check:
 */

RM.Pages.Functions.Reports_List_Install = function(){

};

RM.Pages.Functions.Reports_List_Uninstall = function(){

};
RM.Pages.Functions.Reports_List_Run = function(){
    var selected = RM.Pages.Reports_List_Columns_SM.getSelected();
    RM.Pages.Reports_List_Columns_SM.clearSelections();
    RM.Pages.Functions.Reports_RunJson_Request(selected.data.id);
};

RM.Pages.Functions.Reports_ListJson_Request = function(){
    var myMask = new Ext.LoadMask('rm_pages_reports_page', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Reports',
            action: 'listjson'
        }),
        method: 'POST',
        success: function(responseObject) {
            eval('RM.Pages.Functions.Reports_ListJson('+responseObject.responseText+');');
            myMask.hide();
        },
        failure: function() {
            Ext.Msg.alert('Status', RM.Translate.Common.UnableToShow);
        }
    };
    conn.request(request);
}

RM.Pages.Functions.Reports_ListJson = function (responseObject) {

    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_reports_list_toolbar');

    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_reports_list');
    RM.Pages.Reports_List_Json_Store.load({params:{start: 0, limit: 15}});
    RM.Pages.Reports_List_Grid.render();
}


RM.Pages.Reports_List_Toolbar = {
    xtype : "panel",
    id : "rm_pages_reports_list_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([
        {image: RM.BaseLargeImageURL+"run.gif", label: RM.Translate.Admin.Reports.List.Run, link: "RM.Pages.Functions.Reports_List_Run()"}

        // TODO: add install/uninstall functionality later
        //{image: RM.BaseLargeImageURL+"install.gif", label: RM.Translate.Common.Install, link: "RM.Pages.Functions.Reports_List_Install()"},
        //{image: RM.BaseLargeImageURL+"uninstall.gif", label: RM.Translate.Common.Uninstall, link: "RM.Pages.Functions.Reports_List_Uninstall()"}

    ])
}
RM.Toolbars.push(RM.Pages.Reports_List_Toolbar);


RM.Pages.Reports_List_Json_Store = new Ext.data.JsonStore({
    url: RM.Common.AssembleURL({
        controller: 'Reports',
        action: 'listJson'
    }),
    id: 'rm_reports_list_grid_json_store',
    totalProperty: 'total',
    root: 'data',
    fields: [
        {name: "id"},
        {name: "name"},
        {name: "author"},
        {name: "version"},
        {name: "description"}
    ],
    sortInfo: {field: 'id', direction: 'ASC'},
    remoteSort: true
});


RM.Pages.Reports_List_Columns_SM = new Ext.grid.CheckboxSelectionModel({
    singleSelect: true,
    header: '',
    checkOnly: true
});

RM.Pages.Reports_List_Columns_Rows = [
    RM.Pages.Reports_List_Columns_SM,
    {dataIndex: 'id', header: RM.Translate.Common.Id},
    {dataIndex: 'name', header: RM.Translate.Common.Name},
    {dataIndex: 'author', header: RM.Translate.Admin.Plugins.List.Author},
    {dataIndex: 'version', header: RM.Translate.Admin.Plugins.List.Version},
    {dataIndex: 'description', header: RM.Translate.Admin.Plugins.List.Description}
];

RM.Pages.Reports_List_Columns = new Ext.grid.ColumnModel(RM.Pages.Reports_List_Columns_Rows)

RM.Pages.Reports_List_Columns.defaultSortable = true;
RM.Pages.Reports_List_Filters = new Ext.ux.grid.GridFilters({
    filters: [
        {dataIndex: 'id', type: 'numeric'},
        {dataIndex: 'name', type: 'string'},
        {dataIndex: 'sort_order', type: 'numeric'},
        {dataIndex: 'author', type: 'string'},
        {dataIndex: 'version', type: 'string'},
        {dataIndex: 'description', type: 'string'}
    ]
})

RM.Pages.Reports_List_Grid = new Ext.grid.GridPanel({
    id : 'rm_reports_list_grid',
    plugins: RM.Pages.Reports_List_Filters,
    bbar : new Ext.PagingToolbar({
        store: RM.Pages.Reports_List_Json_Store,
        pageSize: 15,
        plugins: RM.Pages.Reports_List_Filters
    }),
    enableColLock : false,
    loadMask : {
        msg: RM.Translate.Common.PleaseWait
    },
    height : RM.Common.GetPanelHeight(80),
    cm : RM.Pages.Reports_List_Columns,
    ds : RM.Pages.Reports_List_Json_Store,
    sm : RM.Pages.Reports_List_Columns_SM,
    viewConfig: {
        forceFit: true
    },
    autoExpandColumn: 1
});

RM.Pages.Reports_List = new Ext.Panel({
    id : 'rm_pages_reports_list',
    title : RM.Translate.Admin.Reports.List.Title,
    iconCls: "RM_reports_root_icon",
    items : [RM.Pages.Reports_List_Grid]
});

RM.Main.Pages.push(RM.Pages.Reports_List);

